package cn.stylefeng.guns.modular.resultScore.service.Impl;



import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.modular.User.enums.UserEnum;
import cn.stylefeng.guns.modular.question.entity.quenstionnaire;
import cn.stylefeng.guns.modular.resultScore.entity.resultScoreEntity;
import cn.stylefeng.guns.modular.resultScore.mapper.resultScoreMapper;
import cn.stylefeng.guns.modular.resultScore.param.resultScoreParam;
import cn.stylefeng.guns.modular.resultScore.service.resultScoreService;
import cn.stylefeng.guns.sys.modular.emp.service.SysEmpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class resultScoreServiceImpl extends ServiceImpl<cn.stylefeng.guns.modular.resultScore.mapper.resultScoreMapper, resultScoreEntity> implements resultScoreService {

    @Resource
    private SysEmpService sysEmpService;

    @Resource
    private resultScoreMapper resultScoreMapper;

    @Override
    public List<resultScoreEntity> selectUnitService(Integer tid) {

        return resultScoreMapper.selectUnitMapper(tid);
    }

    @Override
    public void add(resultScoreParam resultScoreParam) {
        resultScoreEntity resultScoreEntity = new resultScoreEntity();
        BeanUtil.copyProperties(resultScoreParam,resultScoreEntity);
        this.save(resultScoreEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(resultScoreParam resultScoreParam) {
        resultScoreEntity resultScoreEntity = this.queryCanteenUser(resultScoreParam);


        this.updateById(resultScoreEntity);
        Long id = resultScoreEntity.getId();
        //删除该用户对应的员工表信息
        sysEmpService.deleteEmpInfoByUserId(id);

        this.removeById(id);
    }

    @Override
    public void edit(resultScoreParam resultScoreParam) {
        resultScoreEntity resultScoreEntity = this.queryCanteenUser(resultScoreParam);
        BeanUtil.copyProperties(resultScoreParam,resultScoreEntity);
        this.updateById(resultScoreEntity);
    }


    private resultScoreEntity queryCanteenUser(resultScoreParam resultScoreParam) {
        resultScoreEntity resultScoreEntity = this.getById(resultScoreParam.getId());
        if (ObjectUtil.isEmpty(resultScoreEntity)) {
            throw new ServiceException(UserEnum.NOT_EXISTED);
        }
        return resultScoreEntity;

    }

    @Override
    public int selectAvgNumsService(int min, int max) {
        List<quenstionnaire> list = resultScoreMapper.selectAvgNums(min,max);
        return list.size();
    }
}
